jQuery(function ($) {
    var grid_selector = "#grid-table";
    var pager_selector = "#grid-pager";

    jQuery(grid_selector).jqGrid({
        //direction: "rtl",
        mtype: "POST",//请求的类型：(“POST” or “GET”)	默认GET
        datatype: "json",//表格可以被接受的数据类型：xml，xmlstring，json，local，function
        jsonReader: {root: "dataRows", id: "newsId"},//root:设置记录集的属性名称，id:设置主键的属性名称
        //editurl: cochoApp.baseUrl + "/admin/news/editNewsInfo.do?token=" + cochoApp.getTokenByStorage(),//定义对form编辑时的url（增删改的时候使用）
        url: cochoApp.baseUrl + "/members/news/getDynamicModuleInfoList.do?token=" + cochoApp.getTokenByStorage(),
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: pager_selector,

        altRows: true,
        caption: "新闻动态",
        viewrecords: true,
        height: 322,
        colNames: [ 'ID', '新闻标题','发布时间'],
        colModel: [
            {name: 'newsId', index: 'news_id', hidden: true},
            {name: 'newsTitle', index: 'news_title', width: 100, editable: false, sortable: false,formatter: formartNewsName},
            {name: 'createTime', index: 'create_time', width: 110, editable: false, search: false, type: "date", sorttype: "date", formatter: formartCreateTime}
        ],
        //toppager: true,
        /* multiselect: true,*/
        //multikey: "ctrlKey",
        multiboxonly: true,
        loadError: function (xhr, status, error) {
            cochoAlert.notificationsWarn(cochoErrorMessage.server_error_connect_admin);
        },
        loadComplete: function (xhr) {
            if (xhr != null) {
                if (xhr.statusCode == null || xhr.statusCode == cochoApp.SUCCESS) {
                    var table = this;
                    setTimeout(function () {
                        appJqgrid.styleCheckbox(table);
                        appJqgrid.updateActionIcons(table);
                        appJqgrid.updatePagerIcons(table);
                        appJqgrid.enableTooltips(table);
                    }, 0);
                } else {
                    cochoAlert.notificationsWarn(xhr.statusMsg);
                }
            }
        },
        autowidth: true
    });

    //navButtons
    jQuery(grid_selector).jqGrid('navGrid', pager_selector,
        { 	//navbar options
            edit: false,
            editicon: 'icon-pencil blue',
            add: false,
            addicon: 'icon-plus-sign purple',
            del: false,
            delicon: 'icon-trash red',
            search: true,
            searchicon: 'icon-search orange',
            refresh: true,
            refreshicon: 'icon-refresh green',
            view: false,
            viewicon: 'icon-zoom-in grey'
        },
        {
            //edit record form
            //closeAfterEdit: true,
            recreateForm: true,
            beforeShowForm: function (e) {
                var form = $(e[0]);
                form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />');
                style_edit_form(form);
            }
        },
        {
            //new record form
            closeAfterAdd: true,
            recreateForm: true,
            viewPagerButtons: false,
            beforeShowForm: function (e) {
                var form = $(e[0]);
                form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />');
                style_edit_form(form);
            }
        },
        {
            //delete record form
            recreateForm: true,
            beforeShowForm: function (e) {
                var form = $(e[0]);
                if (form.data('styled')) return false;

                form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />');
                appJqgrid.style_delete_form(form);

                form.data('styled', true);
            },
            onClick: function (e) {
                cochoAlert.notificationsWarn(1);
            }
        },
        {
            //search form
            recreateForm: true,
            afterShowSearch: function (e) {
                var form = $(e[0]);
                form.closest('.ui-jqdialog').find('.ui-jqdialog-title').wrap('<div class="widget-header" />');
                appJqgrid.style_search_form(form);
            },
            afterRedraw: function () {
                appJqgrid.style_search_filters($(this));
            },
            multipleSearch: true
        },
        {
            //view record form
            recreateForm: true,
            beforeShowForm: function (e) {
                var form = $(e[0]);
                form.closest('.ui-jqdialog').find('.ui-jqdialog-title').wrap('<div class="widget-header" />');
            }
        });

    function beforeDeleteCallback(e) {
        var form = $(e[0]);
        if (form.data('styled')) return false;
        form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />');
        appJqgrid.style_delete_form(form);
        form.data('styled', true);
    }

    //显示值的时候做格式化处理
    function formartCreateTime(cellValue, options, cell) {
        return new Date(cellValue).format("yyyy-MM-dd hh:mm:ss");
    }

    function beforeEditCallback(e) {
        var form = $(e[0]);
        form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />');
        style_edit_form(form);
    }

    function style_edit_form(form) {
        //update buttons classes
        form.find('input[name=sex]')
            .addClass('ace ace-switch ace-switch-6')
            .after('<span class="lbl"></span>');

        var buttons = form.next().find('.EditButton .fm-button');
        buttons.addClass('btn btn-sm').find('[class*="-icon"]').remove();//ui-icon, s-icon
        buttons.eq(0).addClass('btn-primary').prepend('<i class="icon-ok"></i>');
        buttons.eq(1).prepend('<i class="icon-remove"></i>');

        buttons = form.next().find('.navButton a');
        buttons.find('.ui-icon').remove();
        buttons.eq(0).append('<i class="icon-chevron-left"></i>');
        buttons.eq(1).append('<i class="icon-chevron-right"></i>');
    }

    function formartNewsName(cellValue, options, cell) {
        return "<a href=\"" + cochoApp.baseUrl + "/control/news/news_display_info.jsp?currentNews=news_display_info&newsId=" + cell.newsId + "\" target='_blank'>" + cellValue + "</a>";
    }

});